package cn.edu.xjtu.hash.hashTable.firstUniqChar;

import java.util.HashMap;
import java.util.Map;

public class Solution {
  public static void main(String[] args) {
    System.out.println(firstUniqChar("loveleetcode"));
  }

  public static int firstUniqChar(String s) {
    Map<Character, Integer> map = new HashMap<>();
    for (int i = 0; i < s.length(); i++) {
      map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1);
    }
    for (char ch : s.toCharArray()) {
      if (map.get(ch) == 1) {
        return s.indexOf(ch);
      }
    }
    return -1;
  }
}
